package com.finace.money.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.util.DateUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.AnalyseListBean;
import com.finace.money.interfaces.OnItemClickListener;
import com.finace.money.ui.adapter.holder.FootViewHolder;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhangpeng on 2017/11/24.
 */

public class NewsAnalyseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int TYPE_HEAD = 0;
    private final int TYPE_BODY = 1;
    private final int TYPE_FOOTER = 6;

    private final int EMPTY = 0;
    //正在加载更多
    private final int LOADING_MORE = 1;
    //没有更多
    private final int NO_MORE = 2;
    //脚布局当前的状态,默认为没有更多
    private int footer_state = 0;

    private List<AnalyseListBean.RowsBean> mDatas;
    private OnItemClickListener mListener;

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }

    public void setmDatas(List<AnalyseListBean.RowsBean> mDatas) {
        this.mDatas = mDatas;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        }else if (null != mDatas && mDatas.size() > 0) {

            List<Integer> parsePositions = parseType();
            if(parsePositions.contains(position)){
                return TYPE_HEAD;
            }else {
                return TYPE_BODY;
            }
        }else {
            return TYPE_HEAD;
        }
    }

    private List<Integer> parseType() {
        HashSet<Integer> dates = new HashSet<>();
        for (int i = 0; i < mDatas.size(); i++) {
            long currentTime = mDatas.get(i).getShow_time();
            String currentDateStr = DateUtil.timeStampToString(currentTime, DateUtil.DATE_TO_STRING_SHORT_PATTERN);
            if(i == 0){
                dates.add(i);
            }else {
                long preTime = mDatas.get(i-1).getShow_time();
                String preDateStr = DateUtil.timeStampToString(preTime, DateUtil.DATE_TO_STRING_SHORT_PATTERN);
                if(!preDateStr.equals(currentDateStr)){
                    dates.add(i);
                }
            }
        }
        return new ArrayList<>(dates);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_FOOTER) {
            return new FootViewHolder(View.inflate(UIUtil.getContext(),
                    R.layout.item_recycler, null));
        } else if (viewType == TYPE_HEAD) {
            return new NewsAnalyseViewHeadHolder(LayoutInflater.from(UIUtil.getContext()).inflate(
                    R.layout.item_home_news_analyse_head, parent, false));
        } else if (viewType == TYPE_BODY) {
            return new NewsAnalyseViewBodyHolder(LayoutInflater.from(UIUtil.getContext()).inflate(
                    R.layout.item_home_news_analyse_body, parent, false));
        } else {
            return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            if (position == 0) {//如果第一个就是脚布局,,那就让他隐藏
                footViewHolder.mProgressBar.setVisibility(View.GONE);
                footViewHolder.tv_line1.setVisibility(View.GONE);
                footViewHolder.tv_line2.setVisibility(View.GONE);
                footViewHolder.tv_state.setText("");
            }
            switch (footer_state) {
                case EMPTY:
                    footViewHolder.itemView.setVisibility(View.GONE);
                    break;
                case LOADING_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.mProgressBar.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line1.setVisibility(View.GONE);
                    footViewHolder.tv_line2.setVisibility(View.GONE);
                    footViewHolder.tv_state.setText(UIUtil.getString(R.string.loading_now));
                    break;
                case NO_MORE:
                    footViewHolder.itemView.setVisibility(View.VISIBLE);
                    footViewHolder.mProgressBar.setVisibility(View.GONE);
                    footViewHolder.tv_line1.setVisibility(View.VISIBLE);
                    footViewHolder.tv_line2.setVisibility(View.VISIBLE);
                    footViewHolder.tv_state.setText(UIUtil.getString(R.string.data_empty));
                    break;
            }

        } else if (holder instanceof NewsAnalyseViewHeadHolder) {
            NewsAnalyseViewHeadHolder newsAnalyseViewHeadHolder = (NewsAnalyseViewHeadHolder) holder;
            AnalyseListBean.RowsBean rowsBean = mDatas.get(position);
            if(null != rowsBean){
                String dateStr = DateUtil.timeStampToString(rowsBean.getShow_time(), DateUtil.DATE_TO_STRING_SHORT_PATTERN);
                String today = DateUtil.timeStampToString(System.currentTimeMillis() / 1000, DateUtil.DATE_TO_STRING_SHORT_PATTERN);
                String hourStr = DateUtil.timeStampToString(rowsBean.getShow_time(), DateUtil.DATE_TO_STRING_PATTERN);

                if(dateStr.equals(today)){
                    newsAnalyseViewHeadHolder.tvItemAnalyseDate.setText(R.string.today);
                }else {
                    newsAnalyseViewHeadHolder.tvItemAnalyseDate.setText(dateStr);
                }
                newsAnalyseViewHeadHolder.tvItemAnalyseTime.setText(hourStr);
                newsAnalyseViewHeadHolder.tvItemAnalyseContent.setText(rowsBean.getContent());
            }else {
                newsAnalyseViewHeadHolder.tvItemAnalyseDate.setText("-");
                newsAnalyseViewHeadHolder.tvItemAnalyseTime.setText("-");
                newsAnalyseViewHeadHolder.tvItemAnalyseContent.setText("-");
            }
            newsAnalyseViewHeadHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != mListener){
                        mListener.onItemClick(view,position);
                    }
                }
            });

        } else if (holder instanceof NewsAnalyseViewBodyHolder) {
            NewsAnalyseViewBodyHolder newsAnalyseViewBodyHolder = (NewsAnalyseViewBodyHolder) holder;
            AnalyseListBean.RowsBean rowsBean = mDatas.get(position);
            if(null != rowsBean){
                String hourStr = DateUtil.timeStampToString(rowsBean.getShow_time(), DateUtil.DATE_TO_STRING_PATTERN);
                newsAnalyseViewBodyHolder.tvItemAnalyseTime.setText(hourStr);
                newsAnalyseViewBodyHolder.tvItemAnalyseContent.setText(rowsBean.getContent());
            }else {
                newsAnalyseViewBodyHolder.tvItemAnalyseTime.setText("-");
                newsAnalyseViewBodyHolder.tvItemAnalyseContent.setText("-");
            }
            newsAnalyseViewBodyHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != mListener){
                        mListener.onItemClick(view,position);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return (null != mDatas && mDatas.size() > 0) ? mDatas.size() +1: 0;
    }

    public class NewsAnalyseViewHeadHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_item_analyse_date)
        TextView tvItemAnalyseDate;
        @BindView(R.id.tv_item_analyse_time)
        TextView tvItemAnalyseTime;
        @BindView(R.id.tv_item_analyse_content)
        TextView tvItemAnalyseContent;

        public NewsAnalyseViewHeadHolder(View inflate) {
            super(inflate);
            ButterKnife.bind(this, inflate);
        }
    }

    public class NewsAnalyseViewBodyHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_item_analyse_time)
        TextView tvItemAnalyseTime;
        @BindView(R.id.tv_item_analyse_content)
        TextView tvItemAnalyseContent;
        public NewsAnalyseViewBodyHolder(View inflate) {
            super(inflate);
            ButterKnife.bind(this, inflate);
        }
    }

    public void changeState(int state) {
        this.footer_state = state;
        notifyDataSetChanged();
    }
}
